﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data.Common;
using System.IO;

namespace SP.OpenSource.CDC.Monitor.BLL
{
    class Utils
    {
        public static DataTable GetProviderFactoryClasses()
        {
            // Retrieve the installed providers and factories.
            DataTable table = DbProviderFactories.GetFactoryClasses();
            return table;
        }
        
        public static bool ParseConnectionString(String sConnectionString, ref String sServerName, ref String sDBName, ref String sProvider,
                                                 ref String sIntegrartedSecurity, ref String sUsername, ref String sPasword,
                                                 ref String sErrorMessage)
        {
            try
            {
                string[] connValues = sConnectionString.Split(';');

                for (int i = 0; i < connValues.Length; i++)
                {
                    string[] keyvaluepair = connValues[i].Split('=');

                    String sKey = String.Empty;
                    String sValue = String.Empty;
                    if (keyvaluepair.Length == 2)
                    {
                        sKey = keyvaluepair[0];
                        sValue = keyvaluepair[1];

                    }

                    switch (sKey.Trim().ToUpper())
                    {
                        case "DATA SOURCE":
                            sServerName = sValue;
                            break;
                        case "PROVIDER":
                            sProvider = sValue;
                            break;
                        case "INITIAL CATALOG":
                            sDBName = sValue;
                            break;
                        case "USER ID":
                            sUsername = sValue;
                            break;
                        case "PASSWORD":
                            sPasword = sValue;
                            break;
                        case "INTEGRATED SECURITY":
                            sIntegrartedSecurity = "True";
                            break;
                    }
                }
                sErrorMessage = String.Empty;
                return true;
            }
            catch (Exception ex)
            {
                sErrorMessage = ex.Message;
                return false;
            }
        }

        public static bool ParseConnectionString(String sConnectionString, ref String sServerName, ref String sDBName, 
                                                 ref String sErrorMessage)
        {
            try
            {
                string[] connValues = sConnectionString.Split(';');

                for (int i = 0; i < connValues.Length; i++)
                {
                    string[] keyvaluepair = connValues[i].Split('=');

                    String sKey = String.Empty;
                    String sValue = String.Empty;
                    if (keyvaluepair.Length == 2)
                    {
                        sKey = keyvaluepair[0];
                        sValue = keyvaluepair[1];

                    }

                    switch (sKey.Trim().ToUpper())
                    {
                        case "DATA SOURCE":
                            sServerName = sValue;
                            break;
                        case "INITIAL CATALOG":
                            sDBName = sValue;
                            break;
                        
                    }
                }
                sErrorMessage = String.Empty;
                return true;
            }
            catch (Exception ex)
            {
                sErrorMessage = ex.Message;
                return false;
            }
        }

        public static String BuildConnectionString(String sServer, String sDBName, bool bIntegratedSecurity, String sUserName,
                                                   String sPassword, String sProvider, ref String sErrorMessage)
        {
            try
            {
                String sConnection = String.Empty;
                sConnection = "Data Source=" + sServer + ";";
                sConnection += "Initial Catalog=" + sDBName + ";";


                if (true == bIntegratedSecurity)
                    sConnection += "Integrated Security=SSPI;";
                else
                {
                    sConnection += "User ID=" + sUserName + ";";
                    sConnection += "Password=" + sPassword + ";";
                }

                if (sProvider.Length > 0)
                    sConnection += "Provider=" + sProvider + ";";

                sErrorMessage = String.Empty;
                return sConnection;
            }
            catch (Exception ex)
            {
                sErrorMessage = ex.Message;
                return String.Empty;
            }
        }
        
        public static String RemoveLastChar(String sInID, String sLastChar)
        {
            try
            {
                int iIndex = sInID.LastIndexOf(sLastChar);
                sInID = sInID.Substring(0, iIndex);
                return sInID;
            }
            catch
            {
                return String.Empty;
            }
        }       

        public static bool GetQuotedString(String sIn, ref String sOut)
        {
            try
            {
                if (sIn.Length > 0)
                {
                    if (sIn.Substring(0,1) != "'")
                        sOut = "'" + sIn;

                    if (sIn.Substring(sIn.Length -1, 1) != "'")
                        sOut = sOut + "'";

                    sOut = sOut.Replace(",", "','");
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

    }
}
